import json
import os
basic_path = str(os.path.dirname(__file__)) + "\\"
job_json_path = basic_path + "..\\..\\school_job\\school_job_detail.json"
score_json_path = basic_path + "..\\..\\school_score\\school_score.json"
detail_path = basic_path + "..\\..\\school_detail\\school_detail.json"

# 打开 JSON 文件
with open(detail_path, 'r', encoding='utf-8') as f:
    data1 = json.load(f)
with open(score_json_path, 'r', encoding='utf-8') as f:
    data2 = json.load(f)
with open(job_json_path, 'r', encoding='utf-8') as f:
    data3 = json.load(f)
# 现在，data 是一个包含 JSON 数据的字典或列表

json_update = {}

for key, value in data1.items():
    print(key)
    print(value['name'])
    is_985 = ''
    is_211 = ''

    if value['f985'] == '1':
        is_985 = '985'
    if value['f211'] == '1':
        is_211 = '211'

    study = 0.0
    life = 0.0
    job = 0.0
    comprehensive = 0.0
    data2_1 = data2.get(key, {})
    if data2_1 != {}:
        study = float(data2_1.get('study', '0.0'))
        life = float(data2_1.get('life', '0.0'))
        job = float(data2_1.get('job', '0.0'))
        comprehensive = float(data2_1.get('comprehensive', '0.0'))

    job_rate = 0.0
    postgraduate = 0.0
    abroad = 0.0
    men_rate = 0.0
    data3_1 = data3.get(key, {})
    if data3_1 != {} and data3_1 != "":
        data3_2 = data3_1.get('jobrate', {})
        if data3_2 != {} and data3_2 != "":
            job_rate = float(data3_2['job']['1'])
            postgraduate = float(data3_2['postgraduate']['1'])
            abroad = float(data3_2['abroad']['1'])
        if data3_1['gradute'] != "":
            men_rate = float(data3_1['gradute'][0]['men_rate'])

    nature = value.get('school_nature_name', '未知')
    print(value['school_nature_name'])
    json_update[key] = {
        'name': value['name'],
        'nature': nature,
        'level_name': value['level_name'],
        'f985': is_985,
        'f211': is_211,
        'dual_class': value['dual_class_name'],
        'study': study,
        'life': life,
        'job': job,
        'comprehensive': comprehensive,
        'content': value['content'],
        'num_lab': int(value['num_lab']),
        'num_subject': int(value['num_subject']),
        'num_master': int(value['num_master']),
        'num_doctor': int(value['num_doctor']),
        'job_rate': job_rate,
        'postgraduate': postgraduate,
        'abroad': abroad,
        'men_rate': men_rate
    }


# 打开一个文件并写入 JSON 数据
with open('json/schoolDetail.json', 'w', encoding='utf-8') as f:
    json.dump(json_update, f, ensure_ascii=False, indent=4)
